package yuejian_07.yuejian_07.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.activity.ConInfoActivity;
import yuejian_07.yuejian_07.javabean.Contest;




public class ContestListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;

    public ContestListAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Contest entity = (Contest) mDataList.get(position);
        if (null == entity)
            return;
        //绑定数据
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.contest_name.setText(entity.getContest_name());
        // System.out.printf("activity_name");
        String contest_pic ="http://10.0.2.2/neu1_yuejian/Public/Admin/photo/"+entity.getContest_pic();

        Picasso.with(mContext).load(contest_pic).into(viewHolder.contest_pic);
        if(viewHolder.contest_pic.getDrawable()==null){
            viewHolder.contest_pic.setImageResource(R.drawable.no_pic);
        }
        //点击item跳转
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putInt("contest_id",entity.getContest_id());
                bundle.putString("contest_name",entity.getContest_name());
                bundle.putString("contest_pic",entity.getContest_pic());
                Intent intent=new Intent(mContext, ConInfoActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_contest, parent, false);
        return new ContestListAdapter.ViewHolder(v);

    }

    //自定义的V
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView contest_pic;
        TextView contest_name;

        public ViewHolder(View itemView) {
            super(itemView);
            contest_name = (TextView) itemView.findViewById(R.id.Contest_name);
            contest_pic = (ImageView) itemView.findViewById(R.id.Contest_pic);
        }
    }


}
